from numpy import random

# 表示每个年龄组的总人数
totals = {20: 0, 30: 0, 40: 0, 50: 0, 60: 0, 70: 0}
# 表示每个年龄组中购买该产品的总数量
purchases = {20: 0, 30: 0, 40: 0, 50: 0, 60: 0, 70: 0}
# 表示收入水平
income_levels = [5000,6000,7000,8000,9000,10000]
# 表示购买的总人数
totalpurchases = 0

# 创建100000个虚拟人
for _ in range(100000):
    agedecade = random.choice([20, 30, 40, 50, 60, 70])  # 随机分配到6个年龄段
    income = random.choice(income_levels)# 随机分配收入到6个年龄段
    purchaseprobability = (float(agedecade)/100.0*0.7+float(income)/10000.0*0.3)  # 假设随着年龄增加，购买产品的概率增加
    totals[agedecade] += 1

    # 判断是否购买产品
    if random.random() < purchaseprobability:
        totalpurchases += 1
        purchases[agedecade] += 1

    # 计算并打印条件概率
for age  in totals:
    conditional_probability = purchases[age] / totals[age] if totals[age] > 0 else 0
    print(f"年龄在{age}岁的人购买产品的条件概率是: {conditional_probability:.4f}")